/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.HomeCommTag;
import com.epfresh.bean.HomeTop;
import com.epfresh.bean.HomeTopBean;
import com.epfresh.utils.TimeStringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class ViewBinderHomeStoreMore
    extends ItemViewBinder<HomeTopBean.StoreMore, ViewBinderHomeStoreMore.ViewHolder> implements View.OnClickListener{
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    int imageWidth;
    int tagImageWidth;
    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if(o instanceof HomeTopBean.StoreMore) {
            HomeTopBean.StoreMore bean = ((HomeTopBean.StoreMore)o);
            MobclickAgent.onEvent(v.getContext(), "Click_Click_HomeRecommend");/*统计广告点击*/
            Intent storeIntent = new Intent(v.getContext(),
                    CommonStoreActivity.class);
            storeIntent.putExtra("storeId", bean.store.getId());
            v.getContext().startActivity(storeIntent);
        }else{
            HomeTopBean.FreshestBean.StoreBean.First3Bean bean = (HomeTopBean.FreshestBean.StoreBean.First3Bean) o;
            MobclickAgent.onEvent(v.getContext(), "Click_Click_HomeRecommend");/*统计广告点击*/
            Intent commIntent = new Intent(v.getContext(),
                    DetailGoodsActivity.class);
            Log.e("id____", bean.getId());
            commIntent.putExtra("storeId", bean.getStoreId());
            commIntent.putExtra("id", bean.getId());
            commIntent.putExtra("image_url", bean.getThumbnail());
            //commIntent.putExtra("promotionItemId", bean.store.getFirst3().get(0).getPromotionItemId());
            commIntent.putExtra("title", bean.getTitle());
            v.getContext().startActivity(commIntent);
        }
    }

    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.view_home_store_more, parent, false);
        imageWidth = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(52))/3;
        tagImageWidth = imageWidth/3;
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeTopBean.StoreMore bean) {

        HomeTopBean.FreshestBean.StoreBean store = bean.store;
        if(store==null)
            return;

        initPicAndTag(holder,store);
        holder.tv_title.setText(bean.store.getName());
        if(bean.store.getStall()==null||"".equals(bean.store.getStall())){
            bean.store.setStall("临时店铺");
        }
        holder.tv_store_info.setText(bean.store.getMarketName()+"  "+bean.store.getStall());
        holder.tv_store_time.setText(TimeStringUtils.initTime(bean.store.getTime(),null));
        holder.itemView.setTag(R.id.item_key_status, bean);
        holder.itemView.setOnClickListener(this);

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_product1,iv_tag1,iv_product2,iv_tag2,iv_product3,iv_tag3;//iv_type;title + drawable? 预售？
        TextView tv_title,tv_store_info,tv_store_time;
        View rl_picture1,rl_picture2,rl_picture3;

        TextView tv_price1, tv_price2, tv_price3;
        TextView tv_price1_, tv_price2_, tv_price3_;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_product1 = (ImageView) itemView.findViewById(R.id.iv_product1);
            iv_tag1 = (ImageView) itemView.findViewById(R.id.iv_tag1);
            iv_product2 = (ImageView) itemView.findViewById(R.id.iv_product2);
            iv_product3 = (ImageView) itemView.findViewById(R.id.iv_product3);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_store_info = (TextView) itemView.findViewById(R.id.tv_store_info);
            tv_store_time = (TextView) itemView.findViewById(R.id.tv_store_time);
            rl_picture1 = itemView.findViewById(R.id.rl_picture1);
            rl_picture2 = itemView.findViewById(R.id.rl_picture2);
            rl_picture3 = itemView.findViewById(R.id.rl_picture3);

            tv_price1 = (TextView) itemView.findViewById(R.id.tv_price1);
            tv_price2 = (TextView) itemView.findViewById(R.id.tv_price2);
            tv_price3 = (TextView) itemView.findViewById(R.id.tv_price3);
            tv_price1_ = (TextView) itemView.findViewById(R.id.tv_price1_);
            tv_price2_ = (TextView) itemView.findViewById(R.id.tv_price2_);
            tv_price3_ = (TextView) itemView.findViewById(R.id.tv_price3_);
        }
    }

    void initPicAndTag(@NonNull ViewHolder holder, @NonNull HomeTopBean.FreshestBean.StoreBean store){
        ViewGroup.LayoutParams params =  holder.iv_product1.getLayoutParams();
        params.width = imageWidth;
        params.height = (int)(imageWidth*0.75);
        holder.iv_product1.setLayoutParams(params);
        holder.iv_product2.setLayoutParams(params);
        holder.iv_product3.setLayoutParams(params);
        ViewGroup.LayoutParams tagParams =  holder.iv_tag1.getLayoutParams();
        tagParams.width = tagImageWidth;
        tagParams.height = tagImageWidth;


        holder.iv_tag1.setLayoutParams(tagParams);
        if(store.getFirst3() == null || store.getFirst3().size() == 0) {
            ImageLoader.getInstance().displayImage("", holder.iv_product1);
            holder.rl_picture3.setVisibility(View.INVISIBLE);
            holder.rl_picture2.setVisibility(View.INVISIBLE);
            holder.tv_price1.setText(store.getFirst3().get(0).getPriceUnit());
            holder.iv_product1.setTag(R.id.item_key_status, store.getFirst3().get(0));
            holder.iv_product1.setOnClickListener(this);
        }


        else if(store.getFirst3().size()>=3){
            ImageLoader.getInstance().displayImage(store.getFirst3().get(0).getThumbnail(), holder.iv_product1);
            ImageLoader.getInstance().displayImage(store.getFirst3().get(1).getThumbnail(), holder.iv_product2);
            ImageLoader.getInstance().displayImage(store.getFirst3().get(2).getThumbnail(), holder.iv_product3);
            holder.rl_picture3.setVisibility(View.VISIBLE);
            holder.iv_product3.setTag(R.id.item_key_status, store.getFirst3().get(2));
            holder.iv_product3.setOnClickListener(this);
            holder.tv_price1.setText(store.getFirst3().get(0).getPrice()+"元/");
            holder.tv_price2.setText(store.getFirst3().get(1).getPrice()+"元/");
            holder.tv_price3.setText(store.getFirst3().get(2).getPrice()+"元/");
            holder.tv_price1_.setText(store.getFirst3().get(0).getChargeUnit());
            holder.tv_price2_.setText(store.getFirst3().get(1).getChargeUnit());
            holder.tv_price3_.setText(store.getFirst3().get(2).getChargeUnit());

            holder.iv_product1.setTag(R.id.item_key_status, store.getFirst3().get(0));
            holder.iv_product1.setOnClickListener(this);
            holder.iv_product2.setTag(R.id.item_key_status, store.getFirst3().get(1));
            holder.iv_product2.setOnClickListener(this);
        }else if(store.getFirst3().size()==2){
            ImageLoader.getInstance().displayImage(store.getFirst3().get(0).getThumbnail(), holder.iv_product1);
            ImageLoader.getInstance().displayImage(store.getFirst3().get(1).getThumbnail(), holder.iv_product2);
            holder.rl_picture3.setVisibility(View.INVISIBLE);

            holder.tv_price1.setText(store.getFirst3().get(0).getPrice()+"元/");
            holder.tv_price2.setText(store.getFirst3().get(1).getPrice()+"元/");
            holder.tv_price1_.setText(store.getFirst3().get(0).getChargeUnit());
            holder.tv_price2_.setText(store.getFirst3().get(1).getChargeUnit());

            holder.iv_product1.setTag(R.id.item_key_status, store.getFirst3().get(0));
            holder.iv_product1.setOnClickListener(this);
            holder.iv_product2.setTag(R.id.item_key_status, store.getFirst3().get(1));
            holder.iv_product2.setOnClickListener(this);

        }else{
            ImageLoader.getInstance().displayImage(store.getFirst3().get(0).getThumbnail(), holder.iv_product1);
            holder.rl_picture3.setVisibility(View.INVISIBLE);
            holder.rl_picture2.setVisibility(View.INVISIBLE);
            holder.tv_price1.setText(store.getFirst3().get(0).getPrice()+"元/");
            holder.tv_price1_.setText(store.getFirst3().get(0).getChargeUnit());
            holder.iv_product1.setTag(R.id.item_key_status, store.getFirst3().get(0));
            holder.iv_product1.setOnClickListener(this);
        }

        if (store.getFirst3().get(0).getPromotionMarkUrl() != null && !"".equals(store.getFirst3().get(0).getPromotionMarkUrl()) && !"null".equals(store.getFirst3().get(0).getPromotionMarkUrl())){
            ImageLoader.getInstance().displayImage(store.getFirst3().get(0).getPromotionMarkUrl(), holder.iv_tag1);
            holder.iv_tag1.setVisibility(View.VISIBLE);
        }
        else
            holder.iv_tag1.setVisibility(View.INVISIBLE);

    }
}
